package es.ats.codington.festival.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.EventSignUpService;
import es.ats.codington.festival.services.PlaceService;

public class ShowEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession sesion = request.getSession(false);
		if (sesion == null){
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/");
	        requestDispatcher.forward(request, response);
		}
		else{	        
	        EventService es=new EventService();
			EventSignUp esu = new EventSignUp();
			EventSignUpService ess = new EventSignUpService();
			PlaceService ps=new PlaceService();
			
			try{
				
			
				int eventId=Integer.parseInt(request.getParameter("eventid"));
				int  visitorId = (Integer) sesion.getAttribute("peopleid");
				esu.setIdEvent(eventId);
				esu.setIdVisitor(visitorId);
				Event e=es.getEventById(eventId);
				Place p=ps.getPlaceById(e.getPlace().getPlaceId());
				
				boolean registered=ess.isRegisteredToEvent(esu);
	
				request.setAttribute("event",e);
				request.setAttribute("registered",registered);
				request.setAttribute("place", p);
				RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/showevent.jsp");
		        requestDispatcher.forward(request, response);
			}
			catch (Exception e) {
				request.setAttribute("error", "Something is wrong with your data. Please, try it again in a few minits.");
				RequestDispatcher rd = request.getRequestDispatcher("jsp/error.jsp");
				rd.forward(request,response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//do nothing
	}
}

